/* 
 * File:   main.c
 * Author: frank
 *
 * Created on January 10, 2015, 5:33 PM
 */

#include <stdio.h>
#include <stdlib.h>
#include "CoreTimerDelay.h"


    #define SYS_FREQ	(40000000)
    #pragma config FNOSC    = PRIPLL        // Oscillator Selection
    #pragma config POSCMOD  = HS            // Primary Oscillator
    #pragma config OSCIOFNC = ON            // CLKO Enable
    #pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider
    #pragma config FPLLIDIV = DIV_2         // PLL Input Divider
    #pragma config UPLLEN   = ON            // USB PLL Enabled
    #pragma config FPLLMUL  = MUL_20        // PLL Multiplier
    #pragma config FPLLODIV = DIV_2         // PLL Output Divider
    #pragma config FPBDIV   = DIV_1         // Peripheral Clock divisor
    #pragma config FWDTEN   = OFF           // Watchdog Timer
    #pragma config WDTPS    = PS1           // Watchdog Timer Postscale
    #pragma config FCKSM    = CSDCMD        // Clock Switching & Fail Safe Clock Monitor
    #pragma config IESO     = OFF           // Internal/External Switch-over
    #pragma config FSOSCEN  = OFF           // Secondary Oscillator Enable
    #pragma config CP       = OFF           // Code Protect
    #pragma config BWP      = OFF           // Boot Flash Write Protect
    #pragma config PWP      = OFF           // Program Flash Write Protect
    #pragma config ICESEL   = ICS_PGx1      // ICE/ICD Comm Channel Select
    #pragma config DEBUG    = OFF           // Debugger Disabled for Starter Kit
    #pragma config JTAGEN   = OFF           // No JTAG.  Need this for RB9 to work!

int cb1 = 0;
int cb2 = 0;
int cbb1 = 0;
int cbb2 = 0;
int failed = 0;

void DelayCallback1(uint8_t id){
    cb1++;
}
void BusyCallback1(bool busy){
    cbb1++;
}
void DelayCallback2(uint8_t id){
    cb2++;
}
void BusyCallback2(bool busy){
    cbb2++;
}

int main(int argc, char** argv) {
    bool result;
    INTEnableSystemMultiVectoredInt();


    CoreDelay_Init();
    CoreDelay_SetDelayCallback(DelayCallback1);
    CoreDelay_SetDelayCallback(DelayCallback2);
    CoreDelay_SetBusyCallback(BusyCallback1);
    CoreDelay_SetBusyCallback(BusyCallback2);

    // client wants 2 delays and needs to manually start the second delay
    result = CoreDelay_Async_us2(true, 100, 200);
    if (!result){
        failed++;
    }
    while (cb1 < 1){}
    result = CoreDelay_Next();
    if (!result){
        failed++;
    }
    while (cb1 < 2){}

    // client is done so this should fail
    result = CoreDelay_Next();
    if (!result){
        failed++;
    }

    // client wants 2 delays and the second delay begins after the first expires
    result = CoreDelay_Async_us2(false, 100, 200);
    if (!result){
        failed++;
    }

    

    bool shouldbebusy = false;
    shouldbebusy = CoreDelay_Busy();    // should be true
    bool busy;
    busy = CoreDelay_Async_us1(100);    // should return false;
    
    while (cb1 < 3){}   // 3 callbacks if successful
    while (cb1 < 4){}   // 4 callbacks if successful

    CoreDelay_Sync_us(1000000);

    return (EXIT_SUCCESS);
}

